import { _decorator, AudioSource, BoxCollider2D, clamp, Component, log, Node, randomRangeInt, tween, UITransform, v3, Vec3 } from 'cc';
import { Battle } from './Battle';
import { Globals } from './Globals';
import { skillType } from './Data/SkillData';
const { ccclass, property } = _decorator;

@ccclass('Frozen')
export class Frozen extends Component {
    // 伤害
    hurt: number = 10;
    level: number = 1;
    // 技能持续时间
    duration: number = 3;

    // private _playerWorldPos: Vec3 = v3();
    private angle: number = randomRangeInt(0, 360) * Math.PI / 180;   // 初始角度
    private radius: number = 50; // 初始半径
    private angleSpeed: number = 2; // 角速度
    private expandSpeed: number = 10; // 半径扩散速度

    onEnable(): void {
        this.angle = randomRangeInt(0, 360) * Math.PI / 180;   // 初始角度
        this.radius = 50; // 初始半径
        this.angleSpeed = clamp(2 + this.level * .5, 2, 10);
        this.expandSpeed = clamp(10 + this.level * .5, 10, 20);

        // 缩放动画
        // this.node.setScale(.1, .1, 1); // 缩放
        tween(this.node)
            .to(this.duration / 2, { scale: v3(clamp(1 + this.level * .1, 1, 2), clamp(1 + this.level * .1, 1, 2), 1) },)
            .delay(this.duration / 4)
            .to(this.duration / 4, { scale: v3(.1, .1, 1) })
            .union()
            .call(() => {
                this.node && Globals.putNode(this.node);
            })
            .start();
    }
    onDisable(): void {
    }
    start() {
    }

    update(deltaTime: number) {
        // 角度随时间变化
        this.angle += this.angleSpeed * deltaTime;

        // 半径随时间扩散
        this.radius += this.expandSpeed * deltaTime;

        // 将极坐标转换为直角坐标
        const x = this.radius * Math.cos(this.angle);
        const y = this.radius * Math.sin(this.angle);

        // 更新节点A的位置，相对于B的位置
        const targetPosition = Battle.playerNode?.worldPosition; // 获取节点B的世界坐标
        const newPosition = v3(targetPosition.x + x, targetPosition.y + y, targetPosition.z);

        // 更新节点的位置
        this.node.setWorldPosition(newPosition);
        // 持续时间减少
        this.duration -= deltaTime;
        // if (this.duration <= 0) {
        //     this.node && Globals.putNode(this.node);
        // }
    }
}


